package cn.com.aiouyi.controller.admin;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.ArticleClass;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.ArticleClassService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by tommy on 2018/4/26.
 */
@Controller("adminArticleClassController")
@RequestMapping("/admin/articleClass")
public class ArticleClassController extends BaseController {

    @Resource(name = "articleClassServiceImpl")
    private ArticleClassService articleClassService;

    @GetMapping("/exist")
    @ResponseBody
    public Message exist(String name) {
        ArticleClass articleClass = articleClassService.findExist(name);
        if (articleClass!=null){
            return Message.ERROR;
        } else {
            return Message.SUCCESS;
        }
    }

    @GetMapping("/list")
    public String list(Pageable pageable, ModelMap model) {

        model.put("page", articleClassService.findPage(pageable));
        return "/admin/articleClass/list";
    }

    @GetMapping("/add")
    public String add() {

        return "/admin/articleClass/add";
    }

    @PostMapping("/save")
    public String save(@Valid ArticleClass articleClass, RedirectAttributes redirectAttributes) {
        ArticleClass pArticleClass = articleClassService.findExist(articleClass.getName());
        if (pArticleClass != null) {
            addFlashMessage(redirectAttributes, Message.error("名称:" + articleClass.getName() + "已存在"));
        } else {
            articleClassService.save(articleClass);
            addFlashMessage(redirectAttributes, Message.SUCCESS);
        }
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(Long id, ModelMap model) {
        if (id != null) {
            model.put("articleClass", articleClassService.find(id));
            return "/admin/articleClass/edit";
        } else {
            model.put(ERROR_VIEW_MESSAGE_KEY, "id不能为空");
            return ERROR_VIEW;
        }
    }

    @PostMapping("/update")
    public String update(@Valid ArticleClass articleClass,
                         ModelMap model, RedirectAttributes redirectAttributes) {
        ArticleClass pArticleClass = articleClassService.findExist(articleClass.getName());
        ArticleClass pArticleClass1 = articleClassService.find(articleClass.getId());
        if (pArticleClass != null && !pArticleClass.getId().equals(pArticleClass1.getId())) {
            addFlashMessage(redirectAttributes, Message.error("名称:" + articleClass.getName() + "已存在"));
        } else {
            articleClassService.update(articleClass);
            addFlashMessage(redirectAttributes, Message.SUCCESS);
        }
        return "redirect:list";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Message delete(Long[] ids, HttpServletRequest request, Model model){
        if (ids != null && ids.length >0){
            articleClassService.delete(ids);
            return Message.SUCCESS;
        } else {
            model.addAttribute(ERROR_VIEW_MESSAGE_KEY, "id不能为空");
            return Message.ERROR;
        }
    }
}
